package com.klm.easymq.controller;

import com.klm.easymq.service.RocketMQProducerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Easy-MQ RocketMQ控制器
 * 提供消息发送的API接口
 * @author wq
 */
@Slf4j
@RestController
@RequestMapping("/easymq/api/rocketmq")
public class EasyMqRocketMQController {
    
    @Autowired
    private RocketMQProducerService rocketMqProducerService;
    
    /**
     * 发送同步消息
     */
    @PostMapping("/send/sync")
    public Map<String, Object> sendSyncMessage(@RequestBody Map<String, String> request) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            String topic = request.get("topic");
            String message = request.get("message");
            
            if (topic == null || message == null) {
                response.put("success", false);
                response.put("message", "topic和message不能为空");
                return response;
            }
            
            boolean result = rocketMqProducerService.sendSyncMessage(topic, message);
            
            response.put("success", result);
            response.put("message", result ? "同步消息发送成功" : "同步消息发送失败");
            response.put("topic", topic);
            response.put("messageContent", message);
            
        } catch (Exception e) {
            log.error("发送同步消息异常", e);
            response.put("success", false);
            response.put("message", "发送同步消息异常: " + e.getMessage());
        }
        
        return response;
    }
    
    /**
     * 发送异步消息
     */
    @PostMapping("/send/async")
    public Map<String, Object> sendAsyncMessage(@RequestBody Map<String, String> request) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            String topic = request.get("topic");
            String message = request.get("message");
            
            if (topic == null || message == null) {
                response.put("success", false);
                response.put("message", "topic和message不能为空");
                return response;
            }

            rocketMqProducerService.sendAsyncMessage(topic, message);
            
            response.put("success", true);
            response.put("message", "异步消息已提交发送");
            response.put("topic", topic);
            response.put("messageContent", message);
            
        } catch (Exception e) {
            log.error("发送异步消息异常", e);
            response.put("success", false);
            response.put("message", "发送异步消息异常: " + e.getMessage());
        }
        
        return response;
    }
    
    /**
     * 发送延迟消息
     */
    @PostMapping("/send/delay")
    public Map<String, Object> sendDelayMessage(@RequestBody Map<String, Object> request) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            String topic = (String) request.get("topic");
            String message = (String) request.get("message");
            Integer delayLevel = (Integer) request.get("delayLevel");
            
            if (topic == null || message == null || delayLevel == null) {
                response.put("success", false);
                response.put("message", "topic、message和delayLevel不能为空");
                return response;
            }
            
            boolean result = rocketMqProducerService.sendDelayMessage(topic, message, delayLevel);
            
            response.put("success", result);
            response.put("message", result ? "延迟消息发送成功" : "延迟消息发送失败");
            response.put("topic", topic);
            response.put("messageContent", message);
            response.put("delayLevel", delayLevel);
            
        } catch (Exception e) {
            log.error("发送延迟消息异常", e);
            response.put("success", false);
            response.put("message", "发送延迟消息异常: " + e.getMessage());
        }
        
        return response;
    }
    
    /**
     * 发送顺序消息
     */
    @PostMapping("/send/ordered")
    public Map<String, Object> sendOrderedMessage(@RequestBody Map<String, String> request) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            String topic = request.get("topic");
            String message = request.get("message");
            String hashKey = request.get("hashKey");
            
            if (topic == null || message == null || hashKey == null) {
                response.put("success", false);
                response.put("message", "topic、message和hashKey不能为空");
                return response;
            }
            
            boolean result = rocketMqProducerService.sendOrderedMessage(topic, message, hashKey);
            
            response.put("success", result);
            response.put("message", result ? "顺序消息发送成功" : "顺序消息发送失败");
            response.put("topic", topic);
            response.put("messageContent", message);
            response.put("hashKey", hashKey);
            
        } catch (Exception e) {
            log.error("发送顺序消息异常", e);
            response.put("success", false);
            response.put("message", "发送顺序消息异常: " + e.getMessage());
        }
        
        return response;
    }
    
    /**
     * 健康检查
     */
    @GetMapping("/health")
    public Map<String, Object> health() {
        Map<String, Object> response = new HashMap<>();
        response.put("status", "UP");
        response.put("service", "RocketMQ");
        response.put("timestamp", System.currentTimeMillis());
        return response;
    }
} 